<template>
  <div ref="chartDom" style="width: 400px; height: 300px;"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'AirChart',
  props: {
    // 接收外部传入的值
    value: {
      type: Number,
      default: 40
    },
    // 是否开启自动随机更新（演示用）
    autoUpdate: {
      type: Boolean,
      default: false
    }
  },
  data()
  {
    return {
      myChart: null,
      option: {
        title: {
          text: '空气浓度',
          left: 'center',
          bottom: '0%',  // 标题放在底部
          textStyle: {
            color: '#333',
            fontSize: 24,
            fontWeight: 'bold'
          }
        },
        series: [
          {
            type: 'gauge',
            center: ['50%', '60%'],
            startAngle: 200,
            endAngle: -20,
            min: 0,
            max: 120,
            splitNumber: 12,
            itemStyle: {
              color: '#9fe080'
            },
            progress: {
              show: true,
              width: 30
            },
            pointer: {
              show: false
            },
            axisLine: {
              lineStyle: {
                width: 30
              }
            },
            axisTick: {
              distance: -45,
              splitNumber: 5,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            splitLine: {
              distance: -52,
              length: 14,
              lineStyle: {
                width: 3,
                color: '#999'
              }
            },
            axisLabel: {
              distance: -20,
              color: '#999',
              fontSize: 20
            },
            anchor: {
              show: false
            },
            title: {
              show: false
            },
            detail: {
              valueAnimation: true,
              width: '70%',
              lineHeight: 36,
              borderRadius: 8,
              offsetCenter: [0, '-15%'],
              fontSize: 26,  // 调整后的字号
              fontWeight: 'bolder',
              formatter: '{value} Ppm',
              color: 'inherit'
            },
            data: [
              {
                value: this.value  // 使用props的初始值
              }
            ]
          },
          {
            type: 'gauge',
            center: ['50%', '60%'],
            startAngle: 200,
            endAngle: -20,
            min: 0,
            max: 120,
            itemStyle: {
              color: '#3ba272'
            },
            progress: {
              show: true,
              width: 8
            },
            pointer: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              show: false
            },
            detail: {
              show: false
            },
            data: [
              {
                value: this.value  // 使用props的初始值
              }
            ]
          }
        ]
      },
      intervalId: null
    }
  },
  watch: {
    // 监听props变化，更新图表
    value(newVal)
    {
      this.updateChart(newVal)
    }
  },
  mounted()
  {
    this.initChart()
    if (this.autoUpdate)
    
      this.startInterval()
  },
  beforeUnmount()
  {
    this.clearInterval()
    if (this.myChart)
    
      this.myChart.dispose()
  },
  methods: {
    initChart()
    {
      this.myChart = echarts.init(this.$refs.chartDom)
      this.myChart.setOption(this.option)
    },
    updateChart(value)
    {
      if (this.myChart)
      {
        this.myChart.setOption({
          series: [
            { data: [{ value }] },
            { data: [{ value }] }
          ]
        })
      }
    },
    startInterval()
    {
      this.intervalId = setInterval(() =>
      {
        const random = +(Math.random() * 60).toFixed(2)
        this.updateChart(random)
      }, 2000)
    },
    clearInterval()
    {
      if (this.intervalId)
      {
        clearInterval(this.intervalId)
        this.intervalId = null
      }
    }
  }
}
</script>
